//
//  StatusPictureView.swift
//  ydy_weibo
//
//  Created by 杨德勇 on 16/7/18.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

import UIKit
import SDWebImage

private let PictureCellId = "PictureCellId"
private let PictureCellMargin: CGFloat = 5



class StatusPictureView: UICollectionView {
    //数据源属性
    var imageURLs : [NSURL]?{
        didSet{
            reloadData()
            //修改自己的frame
            let  viewSize = self.calcuteViewSize()
            
            self.snp_updateConstraints { (make) in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            
            
             textLabel.text = "\(imageURLs?.count ?? 0)"
        }
    }
    
    
    
    //根据图片数量计算自己的大小
    private func calcuteViewSize ()->(CGSize){
        
        let colum :CGFloat = 3.0000001
        let maxWidth = screenW - 2 * PictureCellMargin
        
        let itemWidth = maxWidth - 2 * PictureCellMargin / colum
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
    
        let imagecount = imageURLs?.count ?? 0
        
        //没有图片
        if imagecount == 0 {
            return CGSizeZero
        }
        //单行图片
        if imagecount == 1{
            
            let key = imageURLs?.last?.absoluteString
            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key)
            
            let imagesize = image.size
            layout.itemSize = imagesize
            
            return imagesize
        }
        //双行图片
        if imagecount == 4{
            let w = itemWidth * 2 + PictureCellMargin
            return CGSizeMake(w, w)
        }
        
        //多图
        
        let row = CGFloat((imagecount - 1 ) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        return CGSizeMake(w, h)
        
    }
    
    //MARK:-
    //MARK:3.重写init
    init(){
        
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumLineSpacing = PictureCellMargin
        layout.minimumInteritemSpacing = PictureCellMargin
        //layout做好了，init
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        //注册cell并成为数据源
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        
         dataSource = self
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK:-
    //MARK:2.setup UI
    private func setupUI() {
        
        addSubview(textLabel)
        
        textLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
    }

    
    //MARK:-
    //MARK:1.懒加载控件
    private lazy var textLabel :UILabel = {
       let l = UILabel()
        l.font = UIFont.systemFontOfSize(25)
        l.textColor = UIColor.redColor()
        l.sizeToFit()
        return l
    }()

    

}



//MARK:-
//MARK:collectionview的数据源
extension StatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.imageURL = imageURLs![indexPath.item]
        return cell

    }
}





//MARK:-
//MARK:自定义cell

class PictureCell:UICollectionViewCell{
    //数据源属性
    var imageURL :NSURL?{
        didSet{
            imageView.sd_setImageWithURL(imageURL, placeholderImage: nil)
            
        }
    }
    
    //MARK:-
    //MARK:3.init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK:-
    //MARK:2.setup UI
    private func setupUI(){
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        
        
    }

    
    //MARK:-
    //MARK:1.懒加载控件
    private lazy var imageView: UIImageView = {
        let iv = UIImageView()
        //设置图片样式
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        //手动添加裁剪
        iv.clipsToBounds = true
        return iv
        
    }()

}